<template>
  <div class="app-container">
    <!-- 条件搜索 -->
    <div class="filter-container">
      <el-form ref="queryForm" :model="listQuery.param" size="small" :inline="true" label-width="auto">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="listQuery.param.fileName" placeholder="文件名称" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="所属平台" prop="vendorCode">
          <ft-dict-select v-model="listQuery.param.vendorCode" dict-type="oss_vendor" query-form placeholder="所属平台" style="width: 100%;" class="filter-item" @change="handleFilterQuery" />
        </el-form-item>
        <el-form-item label="租户ID" prop="tenantId">
          <el-input v-model="listQuery.param.tenantId" placeholder="租户ID" class="filter-item" @keyup.enter.native="handleFilterQuery" />
        </el-form-item>
        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-left: 10px;" @click="handleFilterQuery">
          搜索
        </el-button>
        <el-button v-waves class="filter-item" icon="el-icon-refresh-right" @click="handleRestQuery">
          重置
        </el-button>
      </el-form>
    </div>
    <div class="filter-container">
      <div style="float: right;">
        <el-button icon="el-icon-refresh" circle @click="handleFilterQuery" />
      </div>
    </div>

    <!-- 数据表格 -->
    <el-table
      :key="tableKey"
      v-loading="loading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      scrollbar-always-on="true"
    >
      <el-table-column type="selection" width="50px" align="center" />
      <el-table-column label="序号" type="index" width="50px" align="center" />

      <!--按需调整-->
      <el-table-column label="请求流水号" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.requestNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属应用" min-width="90px" align="left">
        <template v-slot="{row}">
          <span>{{ row.serviceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文件名称" min-width="150px" align="left">
        <template v-slot="{row}">
          <span>{{ row.fileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="桶名" min-width="90px" align="left">
        <template v-slot="{row}">
          <span>{{ row.bucketName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="桶存储文件" min-width="180px" align="left">
        <template v-slot="{row}">
          <span>{{ row.bucketFile }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属平台" min-width="80px" align="left">
        <template v-slot="{row}">
          <span>{{ row.vendorName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户ID" min-width="80px" align="left">
        <template v-slot="{row}">
          <span>{{ row.tenantId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" min-width="70px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedBy }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="160px" align="left">
        <template v-slot="{row}">
          <span>{{ row.updatedTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="160px" class-name="small-padding fixed-width">
        <template v-slot="{row,$index}">
          <el-button v-permission="permission.detail" size="mini" type="info" icon="el-icon-document" @click="handleDetail(row, $index)">预览</el-button>
          <el-button v-permission="permission.del" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(row,$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 表格分页 -->
    <pagination :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="handleInit" />
  </div>
</template>

<script>
import { handleDelete, handlePageList, handleSave } from './api'
import { handleFileUrl } from '@/api/ft'
import permission from '@/directive/permission/index' // 权限判断指令
// import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // waves directive
// import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import FtDictSelect from '@/views/components/FtDictSelect/index.vue'

export default {
  // 当前页面名称（组件）
  name: 'FileInfoForm',
  // 引用组件
  components: { FtDictSelect, Pagination },
  directives: { permission, waves },
  filters: {},
  data() {
    return {
      // 列表table的key
      tableKey: 0,
      // 遮罩层
      loading: true,
      // table数据对象
      list: [],
      // 总条数
      total: 0,
      // table条件查询对象
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        // sort: '+id',
        param: {
          // 按需调整
          fileType: undefined,
          fileName: undefined,
          vendorName: undefined,
          tenantId: undefined,
          status: undefined
        }
      },
      // 状态字典
      statusOptions: [{ code: 'Y', text: '启用' }, { code: 'N', text: '禁用' }],
      // 表单数据对象
      formData: {},
      // 是否显示弹出层
      visible: false,
      // 弹出层状态值（新增和修改共用同一个表单页面）
      state: '',
      // 弹出层标题
      title: {
        update: '修改',
        create: '新增',
        detail: '详情'
      },
      // 权限标识
      permission: {
        // 按需补充
        add: ['file:info:add'],
        detail: ['file:info:detail'],
        del: ['file:info:del']
      },
      // 表单校验规则
      rules: {},
      // 详情data
      detail: {
        visible: false,
        data: {}
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.handleInit()
  },
  mounted() {},
  methods: {
    // 查询table数据对象
    handleInit() {
      this.loading = true
      handlePageList(this.listQuery).then(res => {
        this.list = res.data
        if (res.total) {
          this.total = Number(res.total)
        }
        this.loading = false
      })
    },
    // 条件查询table数据
    handleFilterQuery() {
      this.listQuery.pageNum = 1
      this.handleInit()
    },
    // 重置table查询条件
    handleRestQuery() {
      this.$refs.queryForm.resetFields()
    },
    // 更多点击操作项
    handleClick(item, row, index) {
      // 编辑
      if (item === 'edit') {
        this.handleUpdate(row, index)
      }
      // 删除
      if (item === 'del') {
        this.handleDelete(row, index)
      }
    },
    // 新增
    handleCreate() {
      this.state = 'create'
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 详情
    handleDetail(row) {
      const parts = row.bucketFile.split('/')
      // 预览文件
      handleFileUrl(row.bucketName, parts[1]).then(res => {
        window.open(res.data, '_blank')
      })
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 显示加载遮罩
        const loading = this.$loading({ lock: true, text: '处理中...', body: true, fullscreen: false, background: 'rgba(0, 0, 0, 0.05)' })
        handleDelete({ id: row.id }).then((res) => {
          loading.close()
          //  刷新table
          this.handleFilterQuery()
          this.$message({
            showClose: true,
            message: res.msg,
            type: 'success',
            duration: 4000
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 表单提交
    handleSubmitFormData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleSave(this.formData).then((res) => {
            this.handleRestFormData()
            //  刷新table
            this.handleFilterQuery()
            this.visible = false
            this.$message({
              showClose: true,
              message: res.msg,
              type: 'success',
              duration: 4000
            })
          })
        }
      }
      )
    },
    // 关闭弹出层
    handleClose() {
      this.handleRestFormData()
      this.visible = false
    }
  }
}
</script>
